package com.youth.framework.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class VerifyCode
 */
@WebServlet("/pass/getCode")
public class VerifyCode extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public VerifyCode() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		// 设置页面不缓存

		response.setHeader("Pragma", "No-cache");

		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);

		Random random = new Random();
		String str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";

		String num = String.valueOf(str.charAt(random.nextInt(str.length())))
				+ String.valueOf(str.charAt(random.nextInt(str.length())))
				+ String.valueOf(str.charAt(random.nextInt(str.length())))
				+ String.valueOf(str.charAt(random.nextInt(str.length())));
		HttpSession session = request.getSession();
		session.setAttribute("verifyCode", num);

		int width = 120;
		int height = 40;
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);

		// 获取图形上下文
		Graphics2D g = (Graphics2D) image.getGraphics();
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, width, height);

		String str1[] = new String[4];
		Font mFont = new Font("Tahoma", Font.BOLD, 24);
		g.setFont(mFont);
		for (int i = 0; i < str1.length; i++) {
			str1[i] = num.substring(i, i + 1);

			int h = random.nextInt(4) * 6 + 16;

			// 随即生成颜色
			Color color1 = new Color(random.nextInt(120), random.nextInt(120),
					random.nextInt(120));
			g.setColor(color1);
			g.drawString(str1[i], 12 * i + 30, h);
		}
		for (int i = 0; i < 100; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			Color color1 = new Color(random.nextInt(255), random.nextInt(255),
					random.nextInt(255));
			g.setColor(color1); // 随即画各种颜色的点
			g.drawOval(x, y, 0, 0);
		}
		for (int i = 0; i < 18; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int x1 = random.nextInt(width);
			int y1 = random.nextInt(height);
			Color color1 = new Color(random.nextInt(255), random.nextInt(255),
					random.nextInt(255));

			g.setColor(color1); // 随即画各种颜色的线
			g.drawLine(x, y, x1, y1);
		}

		g.dispose();

		ServletOutputStream out = response.getOutputStream();
		ImageIO.write(image, "jpg", out);
		out.flush();
		out.close();

	}

}
